<?php

namespace catchAdmin\externalGoods\controller;

use catchAdmin\externalGoods\model\News as newsModel;
use catchAdmin\externalGoods\model\GoodsAttribute;
use catchAdmin\externalGoods\model\GoodsRule;
use catchAdmin\externalGoods\model\GoodsRule as externalGoodsRuleModel;
use catchAdmin\externalGoods\model\GoodsAttribute as externalGoodsAttribute;
use catchAdmin\externalGoods\request\GoodsCreateRequest;
use catchAdmin\product\service\CategoryService;
use catchAdmin\externalGoods\service\GoodsService as externalGoodsService;
use catcher\base\CatchRequest as Request;
use think\Exception;
use think\facade\Db;
use think\Request as ThinkRequest;
use catcher\CatchAdmin;
use catcher\CatchAuth;
use catcher\CatchResponse;
use houniao\api\v1\Goods as GoodsApi;

class News extends CatchAdmin
{

    public function __construct(newsModel $newsModel)
    {
        $this->newsModel = $newsModel;
    }

    //列表
    public function index(CatchAuth $auth, Request $request)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->newsModel->getList($user['tenant_id']));
    }

    //列表
    public function index1(CatchAuth $auth, Request $request)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->newsModel->getList($user['tenant_id']));
    }

    //列表
    public function index2(CatchAuth $auth, Request $request)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->newsModel->getList($user['tenant_id']));
    }

    //发布&停止
    public function publish(CatchAuth $auth, Request $request){
        $data = $request->post();
        $data['publish'] = $data['status'] == 1 ? 1 : 0;
        return CatchResponse::success($this->newsModel->updateBy($data['id'], $data));
    }

    //修改新闻
    public function update($id, CatchAuth $auth, Request $request)
    {
        $data = $request->put();
        $data['post_time'] = strtotime($data['post_time']);
        unset($data['created_at']);

        if(!empty($data['tags'])){
            $data['tags'] = implode(',',$data['tags']);
        }else{
            $data['tags'] = '';
        }
        return CatchResponse::success($this->newsModel->updateBy($id, $data));
    }

    //删除新闻
    public function delete($id, CatchAuth $auth, Request $request)
    {
        return CatchResponse::success($this->newsModel->deleteBy($id));
    }

    // 新增新闻
    public function create(CatchAuth $auth, Request $request)
    {
        $data = $request->post();
        //判断是否存在post_time 存在就 strtotime 否则就是 time
        if(isset($data['post_time'])){
            $data['post_time'] = strtotime($data['post_time']);
        }else{
            $data['post_time'] = time();
        }

        unset($data['created_at']);

        if(!empty($data['tags'])){
            $data['tags'] = implode(',', $data['tags']);
        }else{
            $data['tags'] = '';
        }
        return CatchResponse::success($this->newsModel->storeBy($data));
    }

    //获取详情
    public function detail($id, CatchAuth $auth, Request $request)
    {
        $data = $this->newsModel->find($id);
        if (!empty($data['tags'])) {
            $data['tags'] = explode(',', $data['tags']);
        } else {
            $data['tags'] = [];
        }
        if (empty($data['publish_title'])) {
            $data['publish_title'] = $data['title'];
        }
        $data['post_time'] = date("Y-m-d H:i:s", $data['post_time']);
        return CatchResponse::success($data);
    }
}